package com.sigmazero13.bgjeeves.widgets;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;

import com.sigmazero13.bgjeeves.R;
import com.sigmazero13.components.MultiRowInput;

public class MultiAutoComplete extends MultiRowInput<AutoCompleteTextView> {

	private ArrayAdapter<String> teamAdapter;
	private Context mContext;
	
	protected class AutoCompleteFactory implements InputFactory<AutoCompleteTextView> {
		@Override
		public AutoCompleteTextView create(Context context) {
			AutoCompleteTextView newView = new AutoCompleteTextView(context);
			newView.setText("");
			newView.setAdapter(teamAdapter);
			LayoutParams lParams = new LayoutParams(0, LayoutParams.WRAP_CONTENT);
			lParams.weight = 1.0f;
			newView.setLayoutParams(lParams);
			//TODO: Set this using the dimension resource
			newView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 14.0f);
			newView.setBackgroundResource(R.drawable.custom_edit_text);
			return newView;
		}
	}
	
	public MultiAutoComplete(Context context) {
		super(context);
		this.attachFactory(new AutoCompleteFactory());
		mContext = context;
	}
	
	public MultiAutoComplete(Context context, AttributeSet attrs) {
		super(context, attrs);
		this.attachFactory(new AutoCompleteFactory());
		mContext = context;
	}
	
	public void setTeamList(List<String> teamList) {
		if (teamList == null) {
			teamList = new ArrayList<String>();
		}
		teamAdapter = new ArrayAdapter<String>(mContext, R.layout.list_item, teamList);
		for (AutoCompleteTextView teamField : inputs) {
			teamField.setAdapter(teamAdapter);
		}
	}
}
